import { request } from '@/service/request';
import { baseUrl } from '@/config/env';
export { Service } from './decorator';

export class BaseService {
    request(options = {}) {
        if (!options.url.includes('http')) {
            let path = '';

            if (process.env.NODE_ENV == 'development') {
                path = this.proxy || baseUrl;
            } else {
                if (this.proxy) {
                    path = this.url;
                } else {
                    path = baseUrl;
                }
            }

            if (this.namespace) {
                path += '/' + this.namespace;
            }

            options.url = path + options.url;
        }

        options.method = options.method || 'GET';

        if (options.method === 'GET' || options.method === 'get') {
            options.data = options.params;
        }

        console.log(`[${options.method}]`, options.url, options.data);
        return request(options);
    }
    list(params) {
        return this.request({
            url: '/list',
            params: {
                ...params
            }
        });
    }
    page(params) {
        return this.request({
            url: '/page',
            params: {
                ...params
            }
        });
    }
    add(params) {
        return this.request({
            url: '/add',
            method: 'POST',
            data: {
                ...params
            }
        });
    }
    update(params) {
        return this.request({
            url: '/update',
            method: 'POST',
            data: {
                ...params
            }
        });
    }
    delete(params) {
        return this.request({
            url: '/delete',
            method: 'POST',
            data: {
                ...params
            }
        });
    }
    info(params) {
        return this.request({
            url: '/info',
            params: {
                ...params
            }
        });
    }
}
